<template lang="pug">
	.form-group
		label.col-md-2.control-label 使用链接下载
		.col-md-10
			p.help-block
				| - 你可以同时添加多个文件下载任务，每行下载一个文件；
				br
				//- | - 你也可以给同一个下载任务添加多个镜像链接，写在一行并用空格分隔每条链接；
				//- br
				| - 链接可以是 HTTP(S)、FTP 和磁力链接。
			textarea.form-control(v-model="uris" lazy)
			br
			div
				include /pugs/setting.pug
</template>
<script>

	const _ = require('lodash')

	module.exports = {
		mixins: [
			require('mixin/rpc'),
			require('mixin/options/index'),
		],
		data () {
			return {
				urisV      : '',
				items      : [],
				disableTabs: [
					'Metalink',
					'RPC',
					'Advanced',
				],
				disableOptions: [
					'log',
					'max-concurrent-downloads',
					'server-stat-of',
					'save-cookies',
					'bt-max-open-files',
					'max-overall-upload-limit',
					'rpc-save-upload-metadata',
					'download-result',
					'keep-unfinished-download-result',
					'log-level',
					'max-download-result',
					'max-overall-download-limit',
					'optimize-concurrent-downloads',
				],
				promise: null,
			}
		},
		methods: {
			submit () {
				if (this.promise && this.promise.isPending()) return

				this.$set(
					'promise',
					this.addUris(this.items)
						.then(res => {
							const arr = []

							res.data.result.forEach((val, index) => {
								if (_.isArray(val)) {
									arr.push(index)
								}
							})

							this.uris = this.uris
								.split('\n')
								.filter((val, index) => !arr.includes(index))
								.join('\n')

							if (this.uris.length === 0) {
								this.$router.go({
									name: 'index',
								})
							}
						})
				)
			},
		},
		computed: {
			uris: {
				get () {
					return this.urisV
				},
				set (value) {
					this.urisV = value

					this.$set(
						'items',
						value.split('\n').map(
							x => [
								[x],
								this.options,
							]
						)
					)
				},
			},
		},
		components: {
			numberInput: require('components/numberInput'),
		},
	}
</script>